using System;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace Camalon.Drawing.Design
{
	/// <summary>
	/// The UITypeEditor of HatchStyle
	/// </summary>
	public class CapStyleEditor : UITypeEditor
		{
			/// <summary>
			/// Constructor
			/// </summary>
			public CapStyleEditor()
			{
			}

			/// <summary>
			/// Overloaded. Gets the editor style used by the EditValue method.
			/// </summary>
			/// <param name="context">An ITypeDescriptorContext
			///    that can be used to gain additional
			///    context information.</param>
			/// <returns>A UITypeEditorEditStyle value that indicates
			///    the style of editor used by the EditValue method.</returns>
			public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
			{
				return UITypeEditorEditStyle.None;
			}

			/// <summary>
			/// Indicates whether the specified context supports painting
			/// a representation of an object's value within the specified context.
			/// </summary>
			/// <param name="context">An ITypeDescriptorContext that
			/// can be used to gain additional context information.</param>
			/// <returns>true if PaintValue is implemented;
			/// otherwise, false.</returns>
			public override bool GetPaintValueSupported(ITypeDescriptorContext context)
			{
				return true;
			}

		
			/// <summary>
			/// Paints a representation of the value of an object
			/// using the specified PaintValueEventArgs.
			/// </summary>
			/// <param name="e">A PaintValueEventArgs that
			/// indicates what to paint and where to paint it.</param>
			public override void PaintValue(PaintValueEventArgs e)
			{
				if (e.Value != null)
				{
					
					CapStyle style = (CapStyle)e.Value;
					Rectangle bounds = e.Bounds;

					int y = bounds.Y + bounds.Height / 2;
					Point start = new Point(bounds.Left, y);
					Point end = new Point(bounds.Right-10, y);
					GraphicsContainer c = e.Graphics.BeginContainer();
					try
					{
						e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
						LineCapStyle ctxStyle = (LineCapStyle)e.Context.Instance;
						using(Outline outline = new Outline(2f))
						{
							LineCapStyle cap = new LineCapStyle(style, CapSize.SmallSmall);
							cap.Align = CapAlign.Center;
							cap.Filled = ctxStyle.Filled;
							//cap.LineInset = ctxStyle.LineInset;
							
							outline.EndCap = cap;

							Pen pen = outline.Pen;
							pen.Color = SystemColors.WindowText;
							pen.MiterLimit = 0;

							using(pen)
							{
//								e.Graphics.FillRectangle(SystemBrushes.Window, bounds);
								e.Graphics.DrawLine(pen, start, end);
							}
						}
					}
					catch( Exception exc)
					{
						FileWatcher.WriteLine("CapStyleEditor::Exception occured" + exc.StackTrace);
					}
					finally
					{
						e.Graphics.EndContainer( c );
					}
				}
			}

		}
}
